package ac.manchester.cs.afzal.term_classifier.term_similarities;

import ac.manchester.cs.afzal.term_classifier.sql_handler.SQL_Handler_Similarities_Impl;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * Title: Term Classifier

 * Derive_Distinct_Sentences class converts the
 * @author Hammad Afzal
 * @version 1.0
 */
public class Contextual_Similarity_Impl
{

    String CONTEXT_SIMILARITY_MODE = "CONTEXT_SIMILARITY_MODE";
    // Nouns/Verbs/Patterns

    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";

    
    String doc_list;
    boolean operation_mode = false;

    private Properties props;

    private Vector documents = new Vector();
    private SQL_Handler_Similarities_Impl sqlHandler;

    public Contextual_Similarity_Impl()
    {
        props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
            database_url = props.getProperty(database_url);
            username = props.getProperty(username);
            password = props.getProperty(password);

            CONTEXT_SIMILARITY_MODE = props.getProperty(CONTEXT_SIMILARITY_MODE);
        }

        catch (FileNotFoundException ex)
        {
            Logger.getLogger(Contextual_Similarity_Impl.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (IOException ex)
        {
            Logger.getLogger(Contextual_Similarity_Impl.class.getName()).log(Level.SEVERE, null, ex);
        }

        sqlHandler = new SQL_Handler_Similarities_Impl();
        sqlHandler.initialize_parameters(database_url, username, password);
        sqlHandler.connect_db();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        String[] tables = new String[1];
        try
        {
            System.out.println("*****************************************************");

            System.out.println("You have opted to perform similarity calculation using : \n" +
                    "********** " + CONTEXT_SIMILARITY_MODE + " ******************\n\n");

            System.out.print("WARNING: Your previous data in all '" + CONTEXT_SIMILARITY_MODE + " Results' \n " +
                    " tables will be lost \n" +
                    " Enter 'y' to continue or 'n' to exit : ");
            input = in.readLine();
            input = input.trim();

            if(input.equals("y"))
            {
                System.out.print("\n\n Are you sure you want to Continue ?  \n" +
                        "  Enter 'y' or 'n' : ");
                input = in.readLine();
                input = input.trim();

                if(input.equals("y"))
                {
                    if(CONTEXT_SIMILARITY_MODE.equals("Nouns"))
                    {
                        tables[0] = "result_noun";
                    }
                    else if(CONTEXT_SIMILARITY_MODE.equals("Verbs"))
                    {
                        tables[0] = "result_verb";
                    }
                    else if(CONTEXT_SIMILARITY_MODE.equals("Patterns"))
                    {
                        tables[0] = "result_pattern";
                    }
                    else
                    {
                        System.out.println("SIMILARITY MODE Value in Properties file not set properly ");
                        System.exit(1);
                    }
                    sqlHandler.delete_all(tables);
                    sqlHandler.reset_auto_inc(tables);
                    System.out.println("Database Reset");
                }
                else
                {
                    System.out.println("You have opted to Quit the program on second thuoght ... ");
                    System.exit(1);
                }
            }
            else
            {
                System.out.println("Program Execution contd without deleting previous data ");
            }
        }
        catch (Exception ex)
        {
            System.out.println("Exception while taking input from user about contextual " +
                    "similarity calculations: " + ex.getMessage());
        }
    }

    public void contextual_similarity()
    {
        if(CONTEXT_SIMILARITY_MODE.equals("Nouns"))
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String input;
            try
            {
                System.out.println("************************************************************");
                System.out.println("Choose 'f' for Full Corpus and 's' for selective:  \n");
                input = in.readLine();
                input = input.trim();

                if(input.equals("f"))
                {
                    operation_mode = true;
                    documents = sqlHandler.get_all_corpus_documents();
                }
                else
                {
                    operation_mode = false;
                    StringBuffer documents_list = new StringBuffer();
                    BufferedReader in_file = new BufferedReader(new FileReader("PMCID.txt"));
                    String str;
                    while((str = in_file.readLine()) != null)
                    {
                        documents_list.append(str);
                        documents_list.append(",");
                    }
                    in.close();
                    doc_list = documents_list.toString();
                    doc_list = doc_list.substring(0, doc_list.length() - 1);
                }
            }
            catch (IOException ex)
            {
                System.out.println("Value of Exception in reading documents list is : " + ex.getMessage());
            }
            if((documents.size() == 0) && (doc_list.equals("")))
            {
                System.out.println("No Documents are in database/file: Me quits");
                System.exit(1);
            }
            if(operation_mode == false)
            {
                sqlHandler.get_selective_corpus_terms(doc_list);
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_nouns();
            }
            else
            {
                sqlHandler.get_all_corpus_terms();
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_nouns();
            }
            sqlHandler.close_db();
        }
        else if(CONTEXT_SIMILARITY_MODE.equals("Verbs"))
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String input;
            try
            {
                System.out.println("************************************************************");
                System.out.println("Choose 'f' for Full Corpus and 's' for selective:  \n");
                input = in.readLine();
                input = input.trim();

                if(input.equals("f"))
                {
                    operation_mode = true;
                    documents = sqlHandler.get_all_corpus_documents();
                }
                else
                {
                    operation_mode = false;
                    StringBuffer documents_list = new StringBuffer();
                    BufferedReader in_file = new BufferedReader(new FileReader("PMCID.txt"));
                    String str;
                    while((str = in_file.readLine()) != null)
                    {
                        documents_list.append(str);
                        documents_list.append(",");
                    }
                    in.close();
                    doc_list = documents_list.toString();
                    doc_list = doc_list.substring(0, doc_list.length() - 1);
                }
            }
            catch (IOException ex)
            {
                System.out.println("Value of Exception in reading documents list is : " + ex.getMessage());
            }
            if((documents.size() == 0) && (doc_list.equals("")))
            {
                System.out.println("No Documents are in database/file: Me quits");
                System.exit(1);
            }
            if(operation_mode == false)
            {
                sqlHandler.get_selective_corpus_terms(doc_list);
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_verbs();
            }
            else
            {
                sqlHandler.get_all_corpus_terms();
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_verbs();
            }
            sqlHandler.close_db();
        }
        else if(CONTEXT_SIMILARITY_MODE.equals("Patterns"))
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String input;
            try
            {
                System.out.println("************************************************************");
                System.out.println("Choose 'f' for Full Corpus and 's' for selective:  \n");
                input = in.readLine();
                input = input.trim();

                if(input.equals("f"))
                {
                    operation_mode = true;
                    documents = sqlHandler.get_all_corpus_documents();
                }
                else
                {
                    operation_mode = false;
                    StringBuffer documents_list = new StringBuffer();
                    BufferedReader in_file = new BufferedReader(new FileReader("PMCID.txt"));
                    String str;
                    while((str = in_file.readLine()) != null)
                    {
                        documents_list.append(str);
                        documents_list.append(",");
                    }
                    in.close();
                    doc_list = documents_list.toString();
                    doc_list = doc_list.substring(0, doc_list.length() - 1);
                }
            }
            catch (IOException ex)
            {
                System.out.println("Value of Exception in reading documents list is : " + ex.getMessage());
            }
            if((documents.size() == 0) && (doc_list.equals("")))
            {
                System.out.println("No Documents are in database/file: Me quits");
                System.exit(1);
            }
            if(operation_mode == false)
            {
                sqlHandler.get_selective_corpus_terms(doc_list);
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_patterns();
            }
            else
            {
                sqlHandler.get_all_corpus_terms();
                sqlHandler.get_all_training_terms();
                sqlHandler.perform_comparison_patterns();
            }
            sqlHandler.close_db();
        }
    }
}